package cn.iocoder.yudao.module.diy.controller.app.diy;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.pojo.*;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.diy.compoent.EventAndSubscribeOfPublisher;
import cn.iocoder.yudao.module.diy.controller.admin.config.vo.GoodsCategoryConfigVo;
import cn.iocoder.yudao.module.diy.controller.app.diy.param.DiyTabbarListParam;
import cn.iocoder.yudao.module.diy.controller.app.diy.vo.SysInitVo;
import cn.iocoder.yudao.module.diy.pojo.*;
import cn.iocoder.yudao.module.diy.service.config.DiyConfigService;
import cn.iocoder.yudao.module.diy.service.diy.IDiyAppService;
import cn.iocoder.yudao.module.diy.utils.RequestUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.experimental.var;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.*;


/**
 * 自定义页面管理
 */
@Schema(description = "用户 App - ns装修页面 Response VO")
@RestController
@RequestMapping("/diy")
public class AppConfigController {
    @Resource
    private IDiyAppService diyAppService;
    //    @Resource
//    private TenantApi tenantApi;
    @Resource
    private DiyConfigService configService;
//    @Resource
//    private MemberLevelApi memberLevelApi;

    /**
     * 获取初始化数据信息
     *
     * @param url
     * @return
     */
    @GetMapping("/init")
    @PermitAll
    public CommonResult<Object> init(@RequestParam(name = "url", defaultValue = "") String url) {
        var vo = new SysInitVo();
        vo.setTabbarList(diyAppService.tabbarList(new DiyTabbarListParam()));
        vo.setMapConfig(configService.map(new SysConfigMapParam()));
        vo.setLoginConfig(configService.getLoginConfig(TenantContextHolder.getTenantId()));
//        vo.setSiteInfo(tenantApi.getTenant(TenantContextHolder.getTenantId()));
        vo.setMemberLevel(null);

//        if (ObjectUtil.isEmpty(vo.getSiteInfo().getId())) {
//            InitWapEvent event = new InitWapEvent();
//            event.setTenantId(TenantContextHolder.getTenantId());
//            EventAndSubscribeOfPublisher.publishAll(event);
//        }

        return successApp(vo);
    }

    /**
     * 登录注册配置
     *
     * @return
     */
    @GetMapping("/login/config")
    @PermitAll
    public CommonResult<LoginConfigVo> getLoginConfig(@RequestParam(name = "url", defaultValue = "") String url) {
        JSONObject config = configService.getConfigValue("LOGIN");
        return successApp(JSONUtil.toBean(config, LoginConfigVo.class));
    }

    @GetMapping("system/tenant/get-by-website")
    @PermitAll
    @TenantIgnore
    public CommonResult<TenantRespVO> getTenantByWebsite1(@RequestParam("website") String website) {
        TenantRespVO tenant = new TenantRespVO();
        tenant.setId(1l);
        tenant.setName("YUDAO");
        return success(tenant);
    }

    /**
     * 获取商品分类配置
     *
     * @return
     */
    @GetMapping("/config/category")
    @PermitAll
    public CommonResult<GoodsCategoryConfigVo> getGoodsCategoryConfig() {
        return successApp(JSONUtil.toBean(configService.getConfigValue("GOODS_CATEGORY_CONFIG"), GoodsCategoryConfigVo.class));
    }

}
